package com.example.project_12;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    private TextView state;
    private EditText ip;
    private EditText port;
    private Button conn;
    private Button disconn;
    private Button open;
    private Button colse;

    private String et_ip;
    private  int et_port;

    private DataOutputStream dataOutputStream = null;
    private static Socket socket = null;
    private  byte[] sendbuff =new byte[5];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        state=(TextView)findViewById(R.id.ster);
        ip=(EditText)findViewById(R.id.ip_tv);
        port=(EditText)findViewById(R.id.port_tv);
        conn=(Button)findViewById(R.id.conn);
        colse=(Button)findViewById(R.id.close);
        disconn=(Button)findViewById(R.id.disconn);
        open=(Button)findViewById(R.id.open);
        conn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startThread();
            }
        });
        colse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            fromData((byte)0);
                            dataOutputStream.write(sendbuff);
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        disconn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(socket!=null && socket.isConnected()){
                    try{
                        socket.close();
                        state.setText("已断开!");
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        });
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            fromData((byte)1);
                            dataOutputStream.write(sendbuff);
                        }catch(IOException e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
    private void startThread()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                et_ip = ip.getText().toString().trim();
                et_port = Integer.parseInt(port.getText().toString().trim());
                try {
                    socket = new Socket(et_ip,et_port);
                    dataOutputStream = new DataOutputStream(socket.getOutputStream());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            state.setText("已连接!");
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void fromData(byte oper)
    {
        sendbuff[0] = (byte)0x55;
        sendbuff[1] = (byte)0xBB;
        sendbuff[2] = (byte)0x00;
        sendbuff[3] = (byte)0x1B;
        sendbuff[4] = (byte)oper;
    }
}
